//
//  Services.m
//  iModern
//
//  Created by Ralf on 5/13/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

#import "Services.h"
#import "AppDelegate.h"
#import "NSDictionary+QueryString.h"

@implementation Services
{
    AppDelegate *appDelegate;
}

- (id)init
{
    appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    return [super init];
}

- (NSURL *)getURL:(NSString *)string
{
    NSString *server = appDelegate.parameterManager.server;

    NSString *urlString = [[NSString alloc] initWithFormat:@"%@/%@", server, string];

    return [[NSURL alloc] initWithString:urlString];
}

- (NSString *)getSessionID
{
    return appDelegate.parameterManager.sessionID;
}

- (ResultSet *)request:(NSString *)string params:(NSDictionary *)params
{
    NSMutableURLRequest *request = [NSMutableURLRequest new];
    [request setURL:[self getURL:string]];
    [request setHTTPMethod:@"POST"];

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPBody:[[params stringWithFormEncodedComponents] dataUsingEncoding:NSUTF8StringEncoding]];

    NSHTTPURLResponse *response = nil;

    NSError *error;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    ResultSet *rs;

    if (responseData)
    {
        NSInteger statusCode = [response statusCode];
        if (statusCode == 200)
        {
            id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
            rs = [[ResultSet alloc] initWithParameters:json statusCode:200 errorCode:0 errorMessage:nil];
        }
        else if (statusCode == 400)
        {
            NSDictionary *error = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];

            rs = [[ResultSet alloc] initWithParameters:nil statusCode:400 errorCode:[error[@"errorCode"] intValue] errorMessage:error[@"errorMessage"]];
        }
    }

    return rs;
}

- (ResultSet *)requestJSON:(NSString *)string params:(NSDictionary *)params
{
    NSMutableURLRequest *request = [NSMutableURLRequest new];
    [request setURL:[self getURL:string]];
    [request setHTTPMethod:@"POST"];

    NSError  *error;
    NSData   *json               = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&error];
    NSString *sPost              = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];

    [request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

    [request setHTTPBody:[[NSMutableData alloc] initWithData:[sPost dataUsingEncoding:NSUTF8StringEncoding]]];

    NSHTTPURLResponse *response     = nil;
    NSData            *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    ResultSet *rs;

    if (responseData)
    {
        NSInteger statusCode = [response statusCode];
        switch (statusCode)
        {
            case 200:
            {
                NSDictionary *article = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
                rs = [[ResultSet alloc] initWithParameters:article statusCode:200 errorCode:0 errorMessage:nil];
                break;
            }
            case 400:
            {
                NSDictionary *error = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
                rs                  = [[ResultSet alloc] initWithParameters:nil statusCode:400 errorCode:[error[@"errorCode"] intValue] errorMessage:error[@"errorMessage"]];
            }
        }
    }

    return rs;
}

- (id)getAppDelegate
{
    return appDelegate;
}

@end
